package com.medica.ledproject.socke;

/**
 * @author xiaona
 * @date 2024/5/30
 * @description TODO
 */

import com.medica.ledproject.led.HexUtil;
import com.medica.ledproject.service.IDzParkZhaService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.InetSocketAddress;

public class SocketHandlerLed extends ChannelInboundHandlerAdapter
{

    private static final Logger log = LoggerFactory.getLogger(SocketHandlerLed.class);

    public static final ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception
    {
        byte[] data = (byte[])(byte[])msg;
        System.out.println(ctx.channel().id().asShortText()+"server channelRead......");
        //System.out.println("----->Server :" + LesShowYQUtil.decode(HexUtil.bytes2HexString(data) ,1,(byte)0x01));
        System.out.println("----->Server :" + HexUtil.bytes2HexString(data));

        for (Channel client : clients) {
           // System.err.println("进来了111");
            if (client.equals(ctx.channel())){
                // System.err.println("进来了");
            }
        }
    }

    public void handlerAdded(ChannelHandlerContext ctx)
            throws Exception
    {

        log.info("新的客户端链接：" + ctx.channel().id().asShortText());
        clients.add(ctx.channel());
    }

    public void handlerRemoved(ChannelHandlerContext ctx)
            throws Exception
    {
        System.err.println("删除");
        clients.remove(ctx.channel());
    }

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
    {
        System.err.println("删除：exceptionCaught");
        cause.printStackTrace();
        ctx.channel().close();
        clients.remove(ctx.channel());
    }
}